﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField] private Vector2 rotateSpeed;
    [SerializeField] private Vector2 cameraEulerXClamp = new Vector2(-89,89);
    [SerializeField] private float moveSpeed;
    [SerializeField] private float jumpStrength;
    [SerializeField] private float pickUpRange;
    
    [SerializeField] private Transform groundCastOrigins;
    [SerializeField] private Transform cameraTransform;
    [SerializeField] private Transform muzzleTransform;
    [SerializeField] private GameObject Bullet;
    [SerializeField] private Transform pickUpParent;
    private Rigidbody playerRigidbody;
    private bool isGrounded;
    private Transform pickedUpObj;

    private float cameraEulerX;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        cameraEulerX = 0;
        cameraTransform.localRotation = Quaternion.Euler(cameraEulerX,0,0);
        playerRigidbody = this.gameObject.GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        HorRotation();
        PlayerMovement();
        Jump();
        PlayerShoot();
        PickUp();
    }

    void HorRotation()
    {
        float t_deltaX = Input.GetAxis("Mouse X");
        float t_deltaY = Input.GetAxis("Mouse Y");
        
        this.transform.Rotate(Vector3.up,t_deltaX*Time.deltaTime*rotateSpeed.x);
        cameraTransform.Rotate(cameraTransform.right,t_deltaY*Time.deltaTime*rotateSpeed.y*-1,Space.World);

        //limit 
        cameraEulerX += t_deltaY * Time.deltaTime * rotateSpeed.y * -1;
        cameraEulerX = Mathf.Clamp(cameraEulerX, cameraEulerXClamp.x, cameraEulerXClamp.y);
        cameraTransform.localRotation = Quaternion.Euler(cameraEulerX,0,0);
    }

    void PlayerMovement()
    {
        Vector3 moveDirection = Vector3.zero;

        if (Input.GetKey(KeyCode.A))
        {
            moveDirection.x -= 1;
        }
        if (Input.GetKey(KeyCode.D))
        {
            moveDirection.x += 1;
        }
        if (Input.GetKey(KeyCode.W))
        {
            moveDirection.z += 1;
        }
        if (Input.GetKey(KeyCode.S))
        {
            moveDirection.z -= 1;
        }

        moveDirection = moveDirection.normalized;
        moveDirection = this.transform.rotation * moveDirection * Time.deltaTime;
        this.gameObject.transform.position += moveDirection*moveSpeed;
        
        playerRigidbody.angularVelocity = Vector3.zero;
    }

    private void Jump()
    {
        Ray groundCast = new Ray(groundCastOrigins.position,Vector3.down);
        RaycastHit groundHitInfo;
        isGrounded = Physics.Raycast(groundCast, out groundHitInfo, 0.208f);
        //Debug.Log(isGrounded);
        //Debug.DrawRay(groundCast.origin,groundCast.direction);
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (isGrounded)
            {
                playerRigidbody.AddForce(Vector3.up*jumpStrength);
                isGrounded = false;
            }
        }
    }

    void PlayerShoot()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(Bullet,muzzleTransform.position,muzzleTransform.rotation);
        }
    }

    private void PickUp()
    {
        Ray pickUpRay = new Ray(cameraTransform.position,cameraTransform.forward);
        RaycastHit pickUpRayHitInfo;
        if (Input.GetMouseButtonDown(1))
        {
            Physics.Raycast(pickUpRay, out pickUpRayHitInfo,pickUpRange);
            if (pickedUpObj == null)
            {
                if (pickUpRayHitInfo.collider.gameObject.tag == "InteractBox")
                {
                    pickedUpObj = pickUpRayHitInfo.transform;
                    Transform boxTransform = pickUpRayHitInfo.transform;
                    pickUpRayHitInfo.transform.SetParent(pickUpParent);
                    //Debug.Log(boxTransform.transform.gameObject.name);
                    boxTransform.transform.localPosition = Vector3.zero;
                    boxTransform.transform.localRotation = Quaternion.identity;
                    if (pickUpRayHitInfo.transform.GetComponent<Rigidbody>())
                    {
                        boxTransform.transform.GetComponent<Rigidbody>().isKinematic = true;
                    }
                    if (pickUpRayHitInfo.transform.GetComponent<Collider>())
                    {
                        pickUpRayHitInfo.transform.GetComponent<Collider>().isTrigger = true;
                    }
                }
                else
                {
                    
                }
            }
            else
            {
                pickedUpObj.SetParent(null);
                if (pickedUpObj.GetComponent<Rigidbody>())
                {
                    pickedUpObj.GetComponent<Rigidbody>().isKinematic = false;
                }
                if (pickedUpObj.GetComponent<Collider>())
                {
                    pickedUpObj.GetComponent<Collider>().isTrigger = false;
                }

                pickedUpObj = null;
            }

        }

    }
}
